package com.catmiw.wyz;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;

import com.catmiw.wyz.util.ExcelUtils;

public class ArithmeticTrainning {

	public static String XLSFILENAME = "D:/王奕卓的教育/基础学习训练/算术基础训练.xls";

	public static void merge(Sheet sheet, int rowb, int rowe, int colb, int cole) {
		sheet.addMergedRegion(new CellRangeAddress(rowb, rowe, colb, cole));
	}

	public static void merge(String content, Sheet sheet, int rowb, int rowe,
			int colb, int cole) {
		sheet.getRow(rowb).getCell(colb).setCellValue(content);
		merge(sheet, rowb, rowe, colb, cole);
	}

	public static void merge(Row row, int colb, int cole) {
		row.getSheet().addMergedRegion(
				new CellRangeAddress(row.getRowNum(), row.getRowNum(), colb,
						cole));
	}

	
	public static void merge(String content, Row row, int colb, int cole) {
		row.getCell(colb).setCellValue(content);
		merge(row, colb, cole);
	}

	public static void merge(int content, Row row, int colb, int cole) {
		row.getCell(colb).setCellValue(content);
		merge(row, colb, cole);
	}

	public static Row newRow(Sheet sheet, int rownum, CellStyle style,
			int rowheight) {
		Row row = sheet.createRow(rownum);
		row.setHeight((short) rowheight);
		for (int i = 0; i < 40; i++) {
			Cell cell = row.createCell(i);
			cell.setCellStyle(style);
		}
		return row;
	}

	public static void createTestPaper(int limit, int count, char pm,
			int result, int rows) {
		String date = DateFormatUtils.format(new Date(), "yyyyMMdd");
		createTestPaper(date, limit, count, pm, result, rows);
	}

	public static void writeEquation(int j, Row row, String str, boolean answer) {
		merge(str, row, j * 8, j * 8 + 4);
		row.getCell(j * 8 + 5).setCellValue("=");
		if (answer) {
			merge(compute(str), row, j * 8 + 6, j * 8 + 7);
		} else {
			merge("", row, j * 8 + 6, j * 8 + 7);
		}

	}

	public static int compute(String equation) {
		String[] t = equation.split(" ");
		int a1 = Integer.parseInt(t[0]);
		int a2 = Integer.parseInt(t[2]);
		return "+".equals(t[1]) ? a1 + a2 : a1 - a2;
	}

	public static void createTestPaper(String date, int limit, int count1,
			char pm, int result, int rows) {
		Workbook wb = ExcelUtils.getWorkbook(XLSFILENAME);
		Font font = wb.createFont();
		font.setFontHeightInPoints((short) 12);
		font.setFontName("Arial");

		Font font2 = wb.createFont();
		font2.setFontHeightInPoints((short) 12);
		font2.setFontName("Arial");
		font2.setStrikeout(true);

		CellStyle style = wb.createCellStyle();
		style.setBorderBottom(CellStyle.BORDER_THIN);
		style.setBorderLeft(CellStyle.BORDER_THIN);
		style.setBorderRight(CellStyle.BORDER_THIN);
		style.setBorderTop(CellStyle.BORDER_THIN);
		style.setAlignment(CellStyle.ALIGN_CENTER);
		style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
		style.setFont(font);

		CellStyle style2 = wb.createCellStyle();
		style2.cloneStyleFrom(style);
		style2.setAlignment(CellStyle.ALIGN_LEFT);

		CellStyle style3 = wb.createCellStyle();
		style3.cloneStyleFrom(style);
		style3.setFont(font2);
		style3.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
		style3.setFillPattern(CellStyle.SOLID_FOREGROUND);

		Sheet sheet = wb.getSheet(date);
		if (sheet == null)
			sheet = wb.createSheet(date);
		wb.setSheetOrder(sheet.getSheetName(), 0);

		Sheet sheet2 = wb.getSheet(date + "答案");
		if (sheet2 == null)
			sheet2 = wb.createSheet(date + "答案");
		wb.setSheetOrder(sheet2.getSheetName(), 1);

		for (int i = 0; i < 40; i++) {
			sheet.setColumnWidth(i, 590);
			sheet2.setColumnWidth(i, 590);
		}

		int rowcount = 0, rowcount2 = 0;

		Row row = newRow(sheet, rowcount++, style, 500);
		merge(date + "算术基础练习", sheet, rowcount - 1, rowcount - 1, 0, 39);

		row = newRow(sheet, rowcount++, style2, 400);
		merge("    一、加减运算", sheet, rowcount - 1, rowcount - 1, 0, 39);

		Row row2 = newRow(sheet2, rowcount2++, style, 500);
		merge(date + "算术基础练习答案（用时:         '         \"，错误：        个）", sheet2,
				rowcount2 - 1, rowcount2 - 1, 0, 39);

		row2 = newRow(sheet2, rowcount2++, style2, 400);
		merge("    一、加减运算", sheet2, rowcount2 - 1, rowcount2 - 1, 0, 39);

		List<String> ls = additionSubtraction(limit, count1);
		for (int i = 0; i < ls.size() / 5; i++) {
			row = newRow(sheet, rowcount++, style, 350);
			row2 = newRow(sheet2, rowcount2++, style, 350);
			for (int j = 0; j < 5; j++) {
				writeEquation(j, row, ls.get(i * 5 + j), false);
				writeEquation(j, row2, ls.get(i * 5 + j), true);
			}
		}

		row = newRow(sheet, rowcount++, style2, 400);
		merge("    二、划出所有 相" + (pm == '+' ? "加" : "减(可向前数借10)") + "得数 的个位等于"
				+ result + "的相邻数字", sheet, rowcount - 1, rowcount - 1, 0, 39);

		row2 = newRow(sheet2, rowcount2++, style2, 400);
		merge("    二、划出所有 相" + (pm == '+' ? "加" : "减") + "得数 的个位等于" + result
				+ "的相邻数字", sheet2, rowcount2 - 1, rowcount2 - 1, 0, 39);

		for (int i = 0; i < rows; i++) {
			row = newRow(sheet, rowcount++, style, 350);
			row2 = newRow(sheet2, rowcount2++, style, 350);
			int tt = 0, iicnt = 0;
			for (int j = 0; j < 36; j++) {
				Cell cell = row.getCell(j);
				int num = RAND.nextInt(10);
				cell.setCellValue(num);
				Cell cell2 = row2.getCell(j);
				cell2.setCellValue(num);
				if (j >= 1) {
					String itt = (pm == '+' ? "" + (tt + num) : ""
							+ (10 + tt - num));
					if (itt.substring(itt.length() - 1, itt.length()).equals(
							"" + result)) {
						cell2 = row2.getCell(j);
						cell2.setCellStyle(style3);
						cell2 = row2.getCell(j - 1);
						cell2.setCellStyle(style3);
						iicnt++;
					}
				}
				tt = num;
			}
			merge("共    组", row, 36, 39);
			merge("共" + iicnt + "组", row2, 36, 39);
		}

		row = newRow(sheet, rowcount++, style2, 400);
		merge("    三、找出能凑成整数的两个数（所有情况）", sheet, rowcount - 1, rowcount - 1, 0,
				39);

		row2 = newRow(sheet2, rowcount2++, style2, 400);
		merge("    三、找出能凑成整数的两个数（所有情况）", sheet2, rowcount2 - 1, rowcount2 - 1,
				0, 39);

		List<Integer> list1 = new ArrayList<Integer>();
		row = newRow(sheet, rowcount++, style, 350);
		row2 = newRow(sheet2, rowcount2++, style, 350);
		for (int i = 0; i < 20; i++) {
			int ii = RAND.nextInt(10) * 10 + RAND.nextInt(9) + 1;
			if (!list1.contains(ii)) {
				merge(ii, row, i * 2, i * 2 + 1);
				merge(ii, row2, i * 2, i * 2 + 1);
				list1.add(ii);
			} else {
				i--;
			}
		}
		ls.clear();
		for (int i = 0; i < list1.size() - 1; i++) {
			for (int j = i + 1; j < list1.size(); j++) {
				int i1 = list1.get(i).intValue();
				int i2 = list1.get(j).intValue();
				if ((i1 + i2) % 10 == 0) {
					ls.add(i1 + " + " + i2);
				}
			}
		}
		for (int i = 0; i < ls.size() / 5 + (ls.size() % 5 == 0 ? 0 : 1); i++) {
			row = newRow(sheet, rowcount++, style, 350);
			row2 = newRow(sheet2, rowcount2++, style, 350);
			for (int j = 0; j < 5; j++) {
				writeEquation(j, row, "", false);
				if (i * 5 + j >= ls.size()) {
					writeEquation(j, row2, "", false);
				} else {
					writeEquation(j, row2, ls.get(i * 5 + j), true);
				}
			}
		}

		ExcelUtils.writeWorkbook(wb, XLSFILENAME);
	}

	public static List<String> createAll(int limit) {
		List<String> temp = new ArrayList<String>();
		for (int i = 0; i <= limit; i++) {
			for (int j = 0; j <= (limit - i); j++) {
				temp.add("" + i + " + " + j);
			}
		}

		for (int i = limit; i >= 0; i--) {
			for (int j = 0; j <= i; j++) {
				temp.add("" + i + " - " + j);
			}
		}

		return temp;
	}

	public static Random RAND = new Random();

	public static List<String> additionSubtraction(int limit, int count) {
		List<String> result = new ArrayList<String>();
		List<String> temp = createAll(limit);

		while (result.size() < count) {
			int rand = RAND.nextInt(temp.size());
			if (!result.contains(temp.get(rand))) {
				result.add(temp.get(rand));
			}
		}
		return result;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		createTestPaper(20, 100, RAND.nextInt(2) == 0 ? '+' : '-',
				RAND.nextInt(10), 5);
	}

}
